package com.ivo.quartz;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ivo.biz.Performance;
import com.ivo.biz.PerformanceMon;
import com.ivo.biz.PerformanceService;
import com.ivo.biz.WHPerformanceManager;
import com.ivo.core.hibernate.HibernateInvoker;
import com.ivo.util.DateUtil;

@Repository
public class WHPerformanceQuartz {
	
	protected Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
 	private HibernateInvoker hibernateInvoker;
	 
    @Autowired
    private PerformanceService performanceService;
	
    @Autowired
    private WHPerformanceManager whPerformanceManager;
    
    
    
    @Transactional
    @Scheduled(cron = "0 10 11 16 * ? ")
	public void execute() {
    	
		
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd");
		Calendar calendar = Calendar.getInstance(); 
		
		String str = sdf.format(calendar.getTime());
		String month = str.substring(0, 7);
				
		calendar.add(Calendar.MONTH, -1);       
		String str1 = sdf.format(calendar.getTime());
		String lastmonth = str1.substring(0, 7);
    
		List<PerformanceMon> list = hibernateInvoker.find(PerformanceMon.class, "from PerformanceMon p where p.factory = 'WH' and p.month = '" + month + "'");
		for(PerformanceMon objPerformanceMon:list){
			
			
			Performance objPerformanceExist = performanceService.getPerformance(month,objPerformanceMon.getEmp_id());
			Performance objPerformanceExistLastmonth = performanceService.getPerformance(lastmonth,objPerformanceMon.getEmp_id());
			if(objPerformanceExist==null){
				Performance objPerformance =new Performance();
				
				objPerformance.setOpe_fk(0L);//Performance OPE_FK 不可为空，赋无意义值
				
				objPerformance.setMonth(objPerformanceMon.getMonth());
				objPerformance.setFactory(objPerformanceMon.getFactory());
				objPerformance.setEmp_id(objPerformanceMon.getEmp_id());
				objPerformance.setEmp_name(objPerformanceMon.getEmp_name());
				objPerformance.setDailywork(20.0);
				objPerformance.setDiscipline(15.0);
				objPerformance.setSafetyatwork(10.0);
				objPerformance.setAudit(5.0);
				objPerformance.setWorkhandover(5.0);
				objPerformance.setAbnormal(5.0);
				objPerformance.setComposite(0.0);
				objPerformance.setAttendance(CalculateAttendance(objPerformanceMon,month));
				
				if(objPerformanceExistLastmonth != null){
					if(objPerformanceExistLastmonth.getGroups() != null){
						objPerformance.setGroups(objPerformanceExistLastmonth.getGroups());
					}
				}
				
				whPerformanceManager.save(objPerformance);
			}else{
				objPerformanceExist.setAttendance(CalculateAttendance(objPerformanceMon,month));
				whPerformanceManager.save(objPerformanceExist);
			}
			
		}
    }
    
    
    
    
    @Transactional
    @Scheduled(cron = "0 15 11 16 * ? ")
    public void CalculateBonus(){
    	String month = DateUtil.formatDate(new Date()).substring(0, 7);
    	List<Performance> list = hibernateInvoker.find(Performance.class,"from Performance p where p.month = '" + month + "'and p.factory = 'WH' and p.isValid is true");
    	if(list != null){
    		for(Performance p : list){
    			
    			
        		double bonus = 0.0;
        		double score = 0.0;
    			double dailywork = p.getDailywork();
    			double attendance = p.getAttendance();
    			double discipline = p.getDiscipline();
    			double safetyatwork = p.getSafetyatwork();
    			double audit = p.getAudit();
    			double workhandover = p.getWorkhandover();
    			double abnormal = p.getAbnormal();
    			double composite = p.getComposite();
    			
    			score = dailywork + attendance + discipline + safetyatwork + audit + workhandover + abnormal;
    			bonus = score + composite;
    			
        		p.setScore(score);
        		p.setBonus(bonus);
        		
        		whPerformanceManager.save(p);
        	}
    	}
    	
    		
    }
    
    
    
    
    @Transactional
    public void CalculateBonus(String month){
    	List<Performance> list = hibernateInvoker.find(Performance.class,"from Performance p where p.month = '" + month + "'and p.factory = 'WH' and p.isValid is true");
    	if(list != null){
    		for(Performance p : list){
    			
    			
        		double bonus = 0.0;
        		double score = 0.0;
    			double dailywork = p.getDailywork();
    			double attendance = p.getAttendance();
    			double discipline = p.getDiscipline();
    			double safetyatwork = p.getSafetyatwork();
    			double audit = p.getAudit();
    			double workhandover = p.getWorkhandover();
    			double abnormal = p.getAbnormal();
    			double composite = p.getComposite();
    			
    			score = dailywork + attendance + discipline + safetyatwork + audit + workhandover + abnormal;
    			bonus = score + composite;
    			
        		p.setScore(score);
        		p.setBonus(bonus);
        		
        		whPerformanceManager.save(p);
        	}
    	}
    	
    		
    }
    
    
    
	public double CalculateAttendance(PerformanceMon objPerformanceMon, String month){
		
		
		double attendance = 15.0;
		Double leavehour = objPerformanceMon.getLeaveHour();
		BigDecimal leavehour1 = new BigDecimal(leavehour);
			Double s= leavehour1.divide(new BigDecimal(8), 0,BigDecimal.ROUND_DOWN).doubleValue();
			attendance = 15.0 - Math.abs(s);
		if(attendance>15.0) attendance=15.0;
		if(attendance<0)  attendance=0.0;
		return attendance;
	}
    
    
		
}
